package cn.itsource.org.service.impl;

import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import cn.itsource.basic.util.AjaxResult;
import cn.itsource.basic.util.BaiduAIUtils;
import cn.itsource.org.domain.Shop;
import cn.itsource.org.domain.ShopAuditLog;
import cn.itsource.org.domain.TEmployee;
import cn.itsource.org.mapper.ShopAuditLogMapper;
import cn.itsource.org.mapper.ShopMapper;
import cn.itsource.org.mapper.TEmployeeMapper;
import cn.itsource.org.service.IShopService;
import cn.itsource.basic.service.impl.BaseImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.mail.internet.MimeMessage;

/**
 * 业务实现类：
 */
@Service
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class ShopServiceImpl extends BaseImpl<Shop> implements IShopService {
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private JavaMailSender sender;
    @Autowired
    private ShopAuditLogMapper shopAuditLogMapper;
    @Autowired
    private TEmployeeMapper tEmployeeMapper;

    @Override
    @Transactional
    public AjaxResult settlment(Shop shop) {
        //校验空值
        if(StrUtil.isBlank(shop.getAddress()) || StrUtil.isBlank(shop.getAdmin().getUsername()) || StrUtil.isBlank(shop.getAdmin().getPassword())){
            throw new RuntimeException("参数不能为空");
        }

        //百度AI审核店铺名称和店铺logo
        if(!BaiduAIUtils.imgCensor(shop.getLogo())){
            throw new RuntimeException("店铺logo不合法");
        }
        if(!BaiduAIUtils.textCensor(shop.getName())){
            throw new RuntimeException("店铺名称不合法");
        }

        //校验密码
        if(!shop.getAdmin().getPassword().equals(shop.getAdmin().getComfirmPassword())){
            throw new RuntimeException("密码不一致");
        }

        //校验店铺是否存在
        Shop chickShop = shopMapper.selectByAddressAndName(shop);
        if(chickShop != null){
            throw new RuntimeException("店铺已存在");
        }

        //加入数据库
        //首先添加店铺
        shopMapper.insert(shop);
        System.out.println(shop.getId());
        TEmployee admin = shop.getAdmin();
        //随机设置盐值
        String salt = RandomUtil.randomString(32);
        admin.setSalt(salt);
        //盐值加密并且重新赋值密码
        String password = SecureUtil.md5(salt + admin.getPassword());
        admin.setPassword(password);
        admin.setState(0);
        admin.setShopId(shop.getId());
        //添加管理员
        tEmployeeMapper.insert(admin);
        System.out.println(admin.getId());
        shop.setAdminId(admin.getId());
        shopMapper.update(shop);

        return AjaxResult.me();

    }

    @Override
    @Transactional
    public AjaxResult auditPass(ShopAuditLog shopAuditLog) {
        //修改shop的状态
        Shop shop = shopMapper.selectById(shopAuditLog.getShopId());
        shop.setState(2);
        shopMapper.update(shop);
        //添加日志
        shopAuditLog.setState(2);
        shopAuditLogMapper.insert(shopAuditLog);
        //尝试发送邮件
        try {
            //创建复杂邮件对象
            MimeMessage mimeMessage = sender.createMimeMessage();
            //发送复杂邮件的工具类
            //true - 能够添加附件
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true,"utf-8");
            helper.setFrom("2687194836@qq.com");
            helper.setSubject("店铺审核反馈");
            helper.setText("<h1>店铺审核已通过</h1><a href='http://localhost:8080/shop/active/"+shopAuditLog.getShopId() + "'>点击激活店铺</a>",true);
            //收件人
            TEmployee employee = tEmployeeMapper.selectById(shop.getAdminId());
            helper.setTo(employee.getEmail());
            sender.send(mimeMessage);
        }catch (Exception e){
            e.printStackTrace();
        }
        return AjaxResult.me();
    }

    @Override
    public AjaxResult auditReject(ShopAuditLog shopAuditLog) {
        //修改shop的状态
        Shop shop = shopMapper.selectById(shopAuditLog.getShopId());
        shop.setState(4);
        shopMapper.update(shop);
        //添加日志
        shopAuditLog.setState(4);
        shopAuditLogMapper.insert(shopAuditLog);

        //尝试发送邮件
        try {
            //创建复杂邮件对象
            MimeMessage mimeMessage = sender.createMimeMessage();
            //发送复杂邮件的工具类
            //true - 能够添加附件
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true,"utf-8");
            helper.setFrom("2687194836@qq.com");
            helper.setSubject("店铺审核反馈");
            helper.setText("<h1>店铺审核失败</h1> <p>失败原因:" + shopAuditLog.getNote() + "</p>",true);
            //收件人
            TEmployee employee = tEmployeeMapper.selectById(shop.getAdminId());
            helper.setTo(employee.getEmail());
            sender.send(mimeMessage);
        }catch (Exception e){
            e.printStackTrace();
        }
        return AjaxResult.me();
    }

    @Override
    public void active(Long shopId) {
        shopMapper.active(shopId);
    }
}
